﻿#include "BrowserTableHandlerNCTAbstract.h"
#include "WidgetBrowserTable.h"
#include "OperatorsInterface/OperAbstractNCT.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsNCT.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include <QMenu>
#include <QColorDialog>

namespace GUI
{
    BrowserTableHandlerNCTAbstract::~BrowserTableHandlerNCTAbstract()
    {
    }

    void BrowserTableHandlerNCTAbstract::contextMenu(QMenu * popMenu)
    {
        if (popMenu == nullptr || _tableWidget == nullptr) return;

        //添加按钮
        QAction* act = this->AddMenuAction(popMenu, tr("Edit"));
        connect(act, SIGNAL(triggered()), this, SLOT(editNCTSlot()));
        //选择一个的时候，不可以批量可以设置属性
        act = this->AddMenuAction(popMenu, tr("Set Attribute"));
        act->setEnabled(this->enableAttriSet());
        connect(act, SIGNAL(triggered()), this, SLOT(setAttributeSlot()));

        act = this->AddMenuAction(popMenu, tr("Duplicate"));
        connect(act, SIGNAL(triggered()), this, SLOT(duplicateNCTSlot()));
        act = this->AddMenuAction(popMenu, tr("Create"));
        connect(act, SIGNAL(triggered()), this, SLOT(createNCTSlot()));
        act = this->AddMenuAction(popMenu, tr("Copy as Text"));
        connect(act, SIGNAL(triggered()), this, SLOT(copyAsTextSlot()));
        popMenu->addSeparator();
        act = this->AddMenuAction(popMenu, tr("Delete"));
        connect(act, SIGNAL(triggered()), this, SLOT(removeNCTSlot()));
    }

    void BrowserTableHandlerNCTAbstract::headerContextMenu(QMenu * popMenu, int coluIndex)
    {
        if (popMenu == nullptr || _tableWidget == nullptr || coluIndex < 0) return;
    }

    EventOper::OperAbstractNCT * BrowserTableHandlerNCTAbstract::getNCTOper()
    {
        //纯虚函数
        return nullptr;
    }

    void BrowserTableHandlerNCTAbstract::editNCTSlot()
    {
        if (_tableWidget == nullptr) return;
        QTableWidgetItem* selectItem = this->getSelectItem();
        if (selectItem == nullptr) return;
        EventOper::OperAbstractNCT* oper = this->getNCTOper();
        if (oper == nullptr) return;
        const int matID = selectItem->type();
        if (matID < 1) return;
        //操作器调用
        oper->editNCT(matID);
    }

    bool BrowserTableHandlerNCTAbstract::enableAttriSet()
    {
        return false;
    }

    void BrowserTableHandlerNCTAbstract::itemDoubleClicked(int row, int colum)
    {
        //双击编辑操作
        if (_tableWidget == nullptr) return;
        this->editNCTSlot();
    }

    void BrowserTableHandlerNCTAbstract::onEditItemColor(QPushButton * button, Acoustics::FITKAcousticsAbsNCT * data)
    {
        if (button == nullptr || data == nullptr) return;
        //修改之前的颜色
        float c[3] = { 0,0,0 };
        data->getColor(c);
        QColor oriC(c[0] * 255, c[1] * 255, c[2] * 255);
        //颜色选择
        QColor color = QColorDialog::getColor(oriC, _tableWidget, tr("Select Fluid Color"));
        if (!color.isValid()) return;
        //修改颜色
        data->setColor(color.redF(), color.greenF(), color.blueF());
        this->SetPushButtonColor(button, color.red(), color.green(), color.blue());
        //刷新渲染
        EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        operGraph->updateAll();
    }

    void BrowserTableHandlerNCTAbstract::createNCTSlot()
    {
        if (_tableWidget == nullptr) return;
        QTableWidgetItem* selectItem = this->getSelectItem();
        if (selectItem == nullptr) return;
        EventOper::OperAbstractNCT* oper = this->getNCTOper();
        if (oper == nullptr) return;
        //操作器调用
        oper->createNCT();
    }

    void BrowserTableHandlerNCTAbstract::duplicateNCTSlot()
    {
        if (_tableWidget == nullptr) return;
        QTableWidgetItem* selectItem = this->getSelectItem();
        if (selectItem == nullptr) return;
        EventOper::OperAbstractNCT* oper = this->getNCTOper();
        if (oper == nullptr) return;
        const int matID = selectItem->type();
        if (matID < 1) return;
        //操作器调用
        oper->duplicateNCT(matID);
    }

    void BrowserTableHandlerNCTAbstract::removeNCTSlot()
    {
        if (_tableWidget == nullptr) return;
        if (_tableWidget->rowCount() <= 1) {
            AppFrame::FITKMessageError("Please keep at least one");
            return;
        }
        QTableWidgetItem* selectItem = this->getSelectItem();
        if (selectItem == nullptr) return;
        EventOper::OperAbstractNCT* oper = this->getNCTOper();
        if (oper == nullptr) return;
        const int matID = selectItem->type();
        if (matID < 1) return;
        //操作器调用
        oper->removeNCT(matID);
    }

    void BrowserTableHandlerNCTAbstract::setAttributeSlot()
    {
        if (_tableWidget == nullptr) return;
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2)return;
        EventOper::OperAbstractNCT* oper = this->getNCTOper();
        if (oper == nullptr) return;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return;
        //名称列
        const int colIndex = item->column();
        QTableWidgetItem* hitem = _tableWidget->horizontalHeaderItem(colIndex);
        if (hitem == nullptr) return;
        const QString text = hitem->text();
        QList<int> itemID;
        //选取的对象根据第一个对象判断
        for (QTableWidgetItem* item : selectItems)
        {
            if (item == nullptr) continue;
            int col = item->column();
            if (col == colIndex)
                //记录对象ID
                itemID.append(item->type());
        }
        //执行批量修改
        oper->setAttribute(text, item->text(), colIndex, itemID);
    }
}